package com.kapo.egroup.security.service;

import com.kapo.egroup.entity.base.user.User;
import com.kapo.egroup.exception.ServiceException;
import com.kapo.egroup.security.domain.LoginUser;
import com.kapo.egroup.service.UserService;
import com.kapo.egroup.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Title UserDetailsServiceImpl
 * @Description TODO
 * @Author 86159
 * @Date 2022-10-16 02:41
 **/
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    @Autowired
    private UserService userService;

    //  username为邮箱或手机号
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.selectByUsername(username);
        if(StringUtils.isNull(user)){
            throw new ServiceException("登录用户：" + username + " 不存在");
        }else{
            return createLoginUser(user);
        }
    }

    public UserDetails createLoginUser(User user){return new LoginUser(user);}
}
